package com.taiyi.renaissancemall.ui.main.first.memberBenefits;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.adapter.AdapterManager;
import com.taiyi.renaissancemall.adapter.baseadapter.CommonAdapter;
import com.taiyi.renaissancemall.bean.MemberBenefitsBean;
import com.taiyi.renaissancemall.bean.MyTeamListBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class MemberBenefitsListAdapter extends BaseQuickAdapter<MemberBenefitsBean.ComlistBean, BaseViewHolder> {
    private Dialog mDialog;
    private TextView nameTv, descriptionTv;
    private LinearLayout container;
    private RecyclerView recyclerView;
    private PagerSnapHelper pagerSnapHelper;
    List<String> imageList = new ArrayList<>();
    List<ImageView> imgList = new ArrayList<>();
    private Context mContext;

    public MemberBenefitsListAdapter(Context context, int layoutResId, @Nullable List<MemberBenefitsBean.ComlistBean> data) {
        super(layoutResId, data);
        mContext = context;
        mDialog = new Dialog(context, R.style.MyDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.hy_dialog_layout, null);
        recyclerView = view.findViewById(R.id.recyclerView);
        nameTv = view.findViewById(R.id.nameTv);
        container = view.findViewById(R.id.container);
        descriptionTv = view.findViewById(R.id.descriptionTv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        pagerSnapHelper = new PagerSnapHelper();
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(true);
    }

    @Override
    protected void convert(BaseViewHolder helper, MemberBenefitsBean.ComlistBean item) {
        ImageView img = helper.getView(R.id.iv_member_item_icon);
        Glide.with(mContext).load(item.smallimgurl).into(img);
        String sName = "";
        if (item.name.contains("\r\n")) {
            sName = item.name.replace("\r\n", "");
        } else {
            sName = item.name;
        }
        helper.setText(R.id.tv_member_item_name, sName);
        helper.setText(R.id.tv_member_item_price, item.zhuanprice);
        TextView buy = helper.getView(R.id.tv_member_item_buy);
        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MemberBenefitsEvent(item, helper.getPosition()));
            }
        });
        ImageView iv = helper.getView(R.id.iv_member_item_icon);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.show();
                nameTv.setText(item.name);
                descriptionTv.setText(item.combz);
                imageList.clear();
                imgList.clear();
                container.removeAllViews();
                if (item.comimgurl.contains(",")) {
                    for (int i = 0; i < item.comimgurl.split(",").length; i++) {
                        imageList.add(item.comimgurl.split(",")[i]);
                        ImageView imageView = new ImageView(mContext);
                        if (i == 0) {
                            imageView.setImageResource(R.mipmap.ind_yes_iv);
                        } else {
                            imageView.setImageResource(R.mipmap.ind_no_iv);
                        }
                        container.addView(imageView);
                        imgList.add(imageView);
                    }
                } else {
                    imageList.add(item.comimgurl);
                    ImageView imageView = new ImageView(mContext);
                    imageView.setImageResource(R.mipmap.ind_yes_iv);
                    container.addView(imageView);
                }

                CommonAdapter imageListAdapter = AdapterManager.getImageListAdapter(mContext, imageList);
                recyclerView.setAdapter(imageListAdapter);
                pagerSnapHelper.attachToRecyclerView(recyclerView);

                recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                            int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
                            Log.i("currentPosition", "----" + firstVisibleItemPosition);
                            for (int i = 0; i < imgList.size(); i++) {
                                if (i == firstVisibleItemPosition) {
                                    imgList.get(i).setImageResource(R.mipmap.ind_yes_iv);
                                } else {
                                    imgList.get(i).setImageResource(R.mipmap.ind_no_iv);
                                }
                            }
                        }
                    }

                    @Override
                    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);

                    }
                });
            }
        });
    }

}
